package com.zx.jhpt.service;

import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.zx.framework.core.util.DatetimeUtils;
import com.zx.framework.core.util.RandomUtil;
import com.zx.framework.core.util.SecurityUtil;
import com.zx.framework.db.IBaseDAO;
import com.zx.framework.plat.dept.po.Dept;
import com.zx.framework.plat.user.po.User;
import com.zx.framework.plat.user.po.UserRole;
import com.zx.framework.plat.user.po.UserRoleId;
import com.zx.jhpt.po.Fxfkjz;
import com.zx.jhpt.po.Replymessage;

@Component("deptTempService")
@Transactional
public class DeptTempService {

    private static final Logger LOGGER = Logger.getLogger(DeptTempService.class);
    @Resource
    private IBaseDAO baseDAO;
    @Resource
    private FxfkjzService fxfkjzService;
    @Resource
    private EnclosureService enclosureService;

    
    
    public void insertUserRole(){
    	String sql = "from User ";
    	List<User> list = baseDAO.queryHQL(sql, null);
    	for (User user : list) {
			UserRole role = new UserRole();
			role.setId(new UserRoleId(user.getId(), "2"));
			baseDAO.save(role);
		}
    }
    
    public void markdata(){
    	try {
    		List<Dept> depts = enclosureService.findDeptByOwner("HB.SJ");
        	Replymessage replymessage;
        	List<Fxfkjz> list = fxfkjzService.getAll();
    		for (Fxfkjz fxfkjz : list) {
    			for (Dept dept : depts) {
        			replymessage = new Replymessage();
        			replymessage.setId(RandomUtil.getUUID());
        			replymessage.setDeptid(dept.getId());
        			replymessage.setDeptname(dept.getName());
        			replymessage.setHcsj(DatetimeUtils.stringToDate("2016-02", "yyyy-MM"));
        			//replymessage.setHcsj(DatetimeUtils.stringToDate("2016-02", "yyyy-MM"));
        			replymessage.setFxid(fxfkjz.getId());
        			
        			
        			
        			baseDAO.save(replymessage);
    			}
    		}
		} catch (Exception e) {
			e.printStackTrace();
		}
    }
    
    public void formatDate(){
    	List<Fxfkjz> list = fxfkjzService.getAll();
		for (Fxfkjz fxfkjz : list) {
			fxfkjz.setBmjkr(fxfkjz.getBmjkr().replaceAll(" ", "").replaceAll("\r\n", "").replaceAll("\r", "").replaceAll("\n", ""));
			fxfkjz.setCjjkpv(fxfkjz.getCjjkpv().replaceAll(" ", "").replaceAll("\r\n", "").replaceAll("\r", "").replaceAll("\n", ""));
			fxfkjz.setFxcyjtms(fxfkjz.getFxcyjtms().replaceAll(" ", "").replaceAll("\r\n", "").replaceAll("\r", "").replaceAll("\n", ""));
			fxfkjz.setFxdmc(fxfkjz.getFxdmc().replaceAll(" ", "").replaceAll("\r\n", "").replaceAll("\r", "").replaceAll("\n", ""));
			fxfkjz.setFxjkkj(fxfkjz.getFxdmc().replaceAll(" ", "").replaceAll("\r\n", "").replaceAll("\r", "").replaceAll("\n", ""));
			fxfkjz.setFxdj(fxfkjz.getFxdj().replaceAll(" ", "").replaceAll("\r\n", "").replaceAll("\r", "").replaceAll("\n", ""));
			fxfkjz.setFxfsqfkcs(fxfkjz.getFxfsqfkcs().replaceAll(" ", "").replaceAll("\r\n", "").replaceAll("\r", "").replaceAll("\n", ""));
			fxfkjz.setFxfshfkcs(fxfkjz.getFxfshfkcs().replaceAll(" ", "").replaceAll("\r\n", "").replaceAll("\r", "").replaceAll("\n", ""));
			fxfkjz.setJccs(fxfkjz.getJccs().replaceAll(" ", "").replaceAll("\r\n", "").replaceAll("\r", "").replaceAll("\n", ""));
			fxfkjz.setMqhcsd(fxfkjz.getMqhcsd().replaceAll(" ", "").replaceAll("\r\n", "").replaceAll("\r", "").replaceAll("\n", ""));
			fxfkjz.setJlbd(fxfkjz.getJlbd().replaceAll(" ", "").replaceAll("\r\n", "").replaceAll("\r", "").replaceAll("\n", ""));
			fxfkjzService.update(fxfkjz);
		}
    }
    
    public void updatePassword(){
    	String hql = "from User ";
    	List<User> list = baseDAO.queryHQL(hql, null);
    	for (User user : list) {
			user.setPassword(SecurityUtil.encryptAES(user.getId()+"123456"));
			baseDAO.update(user);
		}
    }

}
